package alfred.camel;

import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.impl.DefaultEndpoint;
import org.zeromq.ZContext;
import org.zeromq.ZDispatcher;

import java.util.concurrent.TimeUnit;

/**
 */
public class AlfredEndpoint extends DefaultEndpoint {
    private String serviceName;
    private ZContext zmqContext;
    private long timeout = 30000;
    private TimeUnit timeUnit = TimeUnit.MILLISECONDS;
    private boolean transacted = true;
    private ZDispatcher dispatcher;

    protected AlfredEndpoint(String endpointUri, String remaining, AlfredComponent alfredComponent, ZContext zmqContext, ZDispatcher dispatcher) {
        super(endpointUri, alfredComponent);
        this.serviceName = remaining;
        this.zmqContext = zmqContext;
        this.dispatcher = dispatcher;
    }

    @Override
    public Producer createProducer() throws Exception {
        if (transacted) {
            return new TitanicProducer(this, zmqContext, dispatcher, serviceName, timeUnit, timeout);
        }
        return new AlfredProducer(this, zmqContext, dispatcher, serviceName, timeUnit, timeout);
    }

    @Override
    public AlfredConsumer createConsumer(Processor processor) throws Exception {
        return new AlfredConsumer(this, processor, zmqContext, dispatcher, serviceName);
    }

    @Override
    public AlfredComponent getComponent() {
        return (AlfredComponent) super.getComponent();
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    public void setTransacted(boolean transacted) {
        this.transacted = transacted;
    }
}
